package com.lmh.controller;

import com.lmh.pojo.*;

import com.lmh.service.ApplyServie;
import com.lmh.service.DemandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
public class DemandController {

    @Autowired
    private DemandService demandService;
    @Autowired
    private ApplyServie applyServie;

    @RequestMapping("/projecttypequan")
    @ResponseBody
    public List<ProjectType> projecttypequan(){
        List<ProjectType> projectTypes=demandService.projecttypequan();
        return projectTypes;
    }

    @RequestMapping("/technicalquan")
    @ResponseBody
    public List<Technical> technicalquan(){
        List<Technical> technicals=demandService.technicalquan();
        return technicals;
    }

    @RequestMapping("/demandadd")
    public String demandadd(Demand demand, HttpSession session){
        System.out.println(demand);
        System.out.println(demand.getTj());
        Userinfo userinfo=(Userinfo) session.getAttribute("userinfo");
        ProjectType projectType=demandService.projecttypedan(demand.getProjectTypeId());
        String userId=userinfo.getUserId();
        if (demand.getTj().equals("0")){
            demand.setDemandFlag("0");
            String s=UUID.randomUUID().toString();
            String demandId=s.replace("-","");
            demand.setDemandId(demandId);
            demand.setCreateTime(new Date());
            demand.setUpdateTime(new Date());
            demand.setUserId(userId);
            demand.setProjectTypeName(projectType.getProhectTypeName());
            int i=demandService.demandadd(demand);
            if(i !=0){
                return "show";
            }else {
                return "";
            }
        }else {
            demand.setDemandFlag("1");
            String s=UUID.randomUUID().toString();
            String demandId=s.replace("-","");
            demand.setDemandId(demandId);
            demand.setCreateTime(new Date());
            demand.setUpdateTime(new Date());
            demand.setUserId(userId);
            demand.setProjectTypeName(projectType.getProhectTypeName());
            int i=demandService.demandadd(demand);
            if(i !=0){
                return "show";
            }else {
                return "";
            }
        }


    }

    @RequestMapping("/demandquan")
    @ResponseBody
    public List<Demand> demandquan(){
        String flag="0";
        List<Demand> demands=demandService.demandall(flag);
        System.out.println(demands);
        return demands;
    }

    @RequestMapping("/demandlike")
    @ResponseBody
    public List<Demand> demandlike(@RequestBody DpnVo dpnVo,HttpSession session){
        /*int h=demandProjectName.lastIndexOf("}")-1;
        int q=demandProjectName.indexOf(":")+1;
        String dpn=demandProjectName.substring(h,q);*/
        System.out.println("demandProjectName:"+dpnVo.getDemandProjectName());
        session.setAttribute("DemandProjectName",dpnVo.getDemandProjectName());
        List<Demand> demand=demandService.demandlike(dpnVo.getDemandProjectName());
        System.out.println(demand);
            return demand;

    }

    @RequestMapping("/demandprojecttype")
    public String demandprojecttype(Integer prohectTypeId,HttpSession session,Model m){
        //获取session设置的参数
        String demandProjectName=(String) session.getAttribute("DemandProjectName");
        //输出前端获取的参数
        System.out.println("prohectTypeId:"+prohectTypeId);
        //输出此参数
        System.out.println("demandProjectName:"+demandProjectName);
        if(demandProjectName !=null){
        //调用service方法查询数据库
            List<Demand> demands=demandService.demandprojecttype(prohectTypeId,demandProjectName);
            //Moder设置返回的集合
            m.addAttribute("demands",demands);
            //返回页面
            return "demandhall";
        }else {
            demandProjectName="";
            List<Demand> demands=demandService.demandprojecttype(prohectTypeId,demandProjectName);
            m.addAttribute("demands",demands);
            return "demandhall";
        }
    }

    @RequestMapping("/demand")
    public String demand(){
        return "demand";
    }

    @RequestMapping("/demandhall")
    public String demandhall(Model m){
        String flag="0";
        List<Demand> demands=demandService.demandall(flag);
        m.addAttribute("demands",demands);
        return "demandhall";
    }

    @RequestMapping("/demanddan")
    public String demanddan(String demandId,Model m){

        Demand demand=demandService.demanddan(demandId);
        List<Userinfo> demandApplies=applyServie.applyall(demandId);
        System.out.println(demand);
        if(demand !=null){
            m.addAttribute("demand",demand);
            m.addAttribute("demandApplies",demandApplies);
            return "demanddan";
        }else {
            return "demanddan";
        }
    }





}
